﻿using DataAccess;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class UsersBusiness
    {
        public object GetCount()
        {
            object res = null;
            string query = "select count(1) from users";
            res = DA.GetOneData(query, CommandType.Text, null, null);
            return res;
        }

        public DataTable GetAllData()
        {
            DataTable dt = null;
            string query = "select * from users";
            dt = DA.GetDataTable(query, CommandType.Text, null, null);
            return dt;
        }

        public DataTable SearchAllData(UsersEntity ue)
        {
            DataTable dt = null;
            string query = "select * from users where username like @username";
            string[] paras = { "@username" };
            object[] values = { "%" + ue.Username + "%" };
            dt = DA.GetDataTable(query, CommandType.Text, paras, values);
            return dt;
        }

        public int AddUser(UsersEntity ue)
        {
            int res = 0;
            string query = "insert into users values (@username,@password,@address,@telephone)";
            string[] paras = { "@username", "@password", "@address", "@telephone" };
            object[] values = { ue.Username, ue.Password, ue.Address, ue.Telephone };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public int DeleteUser(UsersEntity ue)
        {
            int res = 0;
            string query = "delete from users where userid = @userid";
            string[] paras = { "@userid" };
            object[] values = { ue.Userid };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public int EditUser(UsersEntity ue)
        {
            int res = 0;
            string query = "update users set username=@username,password=@password,address=@address,telephone=@telephone where userid=@userid";
            string[] paras = { "@username", "@password", "@address", "@telephone", "@userid" };
            object[] values = { ue.Username, ue.Password, ue.Address, ue.Telephone, ue.Userid };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public int ChangeUserPassword(UsersEntity ue)
        {
            int res = 0;
            string query = "uodate users set password=@password where username = @username";
            string[] paras = { "@password", "@username" };
            object[] values = { ue.Password, ue.Username };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public int ChangeUserInfo(UsersEntity ue)
        {
            int res = 0;
            string query = "update users set address = @address, telephone = @telephone where username = @username";
            string[] paras = { "@address", "@telephone", "@username" };
            object[] values = { ue.Address, ue.Telephone, ue.Username };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public object GetPasswordByUserName(UsersEntity ue)
        {
            object res = null;
            string query = "select password from users where username = @username";
            string[] paras = { "@username" };
            object[] values = { ue.Username };
            res = DA.GetOneData(query, CommandType.Text, paras, values);
            return res;
        }

        public DataTable GetUserInfo(UsersEntity ue)
        {
            DataTable dt = null;
            string query = "select * from users where username = @username";
            string[] paras = { "@username" };
            object[] values = { ue.Username };
            dt = DA.GetDataTable(query, CommandType.Text, paras, values);
            return dt;
        }

        public object CheckExist(UsersEntity ue)
        {
            object res = null;
            string query = "select * from users where username = @username and telephone = @telephone";
            string[] paras = { "@username", "@telephone" };
            object[] values = { ue.Username, ue.Telephone };
            res = DA.GetOneData(query, CommandType.Text, paras, values);
            return res;
        }

        public int FrogetPassword(UsersEntity ue)
        {
            int res = 0;
            string query = "update users set password = @password where username = @username and telephone = @telephone";
            string[] paras = { "@password", "@username", "@telephone" };
            object[] values = { ue.Password, ue.Username, ue.Telephone };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }
    }
}
